using System;
using System.Collections.Generic;
using System.Text;
using Xunit;
using EventAbstractionAPI.RuleEngine;
using System.Threading;

//The commandline for the xunit app, to test this assebly.
//C:\Program Files\xunit>xunit.console.exe "C:\Documents and Settings\rhwilbur\My Documents\Visual Studio 2005\Projects\TestWorkFlowLibrary\WorkFlowTests\bin\Debug\WorkFlowTests.dll"
namespace WorkFlowTests
{
    public class OrEventUseCase
    {

        #region Constructor
        public OrEventUseCase()
        {
            _eventalgebra.RaiseEvent += new Fire(_eventalgebra_RaiseEvent);
        }

        void _eventalgebra_RaiseEvent(List<ICapturedEvent> expiredEvents)
        {
            Console.WriteLine("The Output:");
            foreach (ICapturedEvent c in expiredEvents)
            {
                Console.WriteLine(":" + c.EventName + c.ComplexName);
            }
            _eventFired = true;
            _receivedResults.AddRange(expiredEvents);
        } 
        #endregion

        /// <summary>
        /// check to see if we get any response from an event being raised. based on inputing some events.
        /// </summary>
        [Fact]
        public void Test1()
        {
            //add a complex event. E1 <-- E2;E3
            _eventalgebra.Register(new OrEvent(new TimeSpan(0, 0, 0, 0,100),"ComplexEvent1", "eventType2", "eventType3"));
            //_eventalgebra.Register(new OrEvent(new TimeSpan(0, 0, 0, 1), "ComplexEvent2", "eventType5", "eventType6"));

            ICapturedEvent event1 = new CapturedEvent();
            event1.EventName = "eventType1";

            ICapturedEvent event2 = new CapturedEvent();
            event2.EventName = "eventType2";

            ICapturedEvent event3 = new CapturedEvent();
            event3.EventName = "eventType3";

            ICapturedEvent event4 = new CapturedEvent();
            event4.EventName = "eventType4";

            ICapturedEvent event5 = new CapturedEvent();
            event5.EventName = "eventType5";

            ICapturedEvent event6 = new CapturedEvent();
            event6.EventName = "eventType6";

            _eventalgebra.Receive(event1);
            _eventalgebra.Receive(event2);
            _eventalgebra.Receive(event3);
            _eventalgebra.Receive(event4);
            _eventalgebra.Receive(event5);
            Thread.Sleep(100);
            _eventalgebra.Receive(event6);

            _expectedResults.Add("eventType1");
            _expectedResults.Add("ComplexEvent1");
            _expectedResults.Add("ComplexEvent1");
            _expectedResults.Add("eventType4");
            _expectedResults.Add("eventType5");
            _expectedResults.Add("eventType6");

            int i = 0;
            while (_receivedResults.Count < _expectedResults.Count)
            {
                Thread.Sleep(100); // pause for 1 second = 1000;

                if (i > 8)
                {
                    break;
                }
                else
                {
                    i++;
                }
            }

            Assert.True(_eventFired, "No events were received");

            //compare expect results. with streamed results.

            Assert.True(_receivedResults.Count == _expectedResults.Count, "Different number of results than expected. We got: " + _receivedResults.Count.ToString() + "we expected: " + _expectedResults.Count.ToString());

            for (int j = 0; j < _expectedResults.Count; j++)
            {
                if (!(_expectedResults[j] == _receivedResults[j].ComplexName || _expectedResults[j] == _receivedResults[j].EventName))
                {
                    _ResultsCorrect = false;
                    break;
                }
            }

            Assert.True(_ResultsCorrect,
                        "The results arent as expected. Event Expirery could cause this, check the life times for your event types. We receieved the following " + _receivedResults.Count.ToString() + " items:" + _receivedResults[0].EventName + _receivedResults[0].ComplexName + "," + _receivedResults[1].EventName + _receivedResults[1].ComplexName + "," + _receivedResults[2].EventName + _receivedResults[2].ComplexName);

        }

        List<String> _expectedResults = new List<string>();
        List<ICapturedEvent> _receivedResults = new List<ICapturedEvent>();
        Boolean _eventFired = false;
        Boolean _ResultsCorrect = true;
        IEventAlgebra _eventalgebra = new EventAlgebra();
    }
}
